part of '../repository.dart';

const _kPurchasedSubscriptionInfoAppStoreUserIdKey = 'app_store_user_id';
const _kPurchasedSubscriptionInfoPurchaseItemSkuKey = 'purchase_item_sku';
const _kPurchasedSubscriptionInfoOriginalTransactionIdKey = 'original_transaction_id';
const _kPurchasedSubscriptionInfoAutoRenewalEnabledKey = 'auto_renewal_status';
const _kPurchasedSubscriptionInfoIsInBillingRetryPeriodKey = 'is_in_billing_retry_period';
const _kPurchasedSubscriptionInfoPriceIncreaseStatusKey = 'price_increase_status';
const _kPurchasedSubscriptionInfoTestKey = 'test';
const _kPurchasedSubscriptionInfoExpirationReasonKey = 'expiration_reason';
const _kPurchasedSubscriptionInfoGracePeriodExpirationTimeKey = 'grace_period_expiration_time';

class _PurchasedSubscriptionInfoDto extends Equatable {
  const _PurchasedSubscriptionInfoDto._internal({
    required this.appStoreUserId,
    required this.purchaseItemSku,
    required this.originalTransactionId,
    required this.autoRenewalEnabled,
    required this.isInBillingRetryPeriod,
    required this.priceIncreaseStatusDto,
    required this.test,
    required this.expirationReasonDto,
    required this.gracePeriodExpirationTimeDto,
  });

  final String appStoreUserId;
  final String purchaseItemSku;
  final String originalTransactionId;
  final bool autoRenewalEnabled;
  final bool isInBillingRetryPeriod;
  final _PurchasedSubscriptionPriceIncreaseStatusDto priceIncreaseStatusDto;
  final bool test;
  final _PurchasedSubscriptionExpirationReasonDto? expirationReasonDto;
  final _TimeDto? gracePeriodExpirationTimeDto;

  @override
  List<Object?> get props {
    return [
      appStoreUserId,
      purchaseItemSku,
      originalTransactionId,
      autoRenewalEnabled,
      isInBillingRetryPeriod,
      priceIncreaseStatusDto,
      test,
      expirationReasonDto,
      gracePeriodExpirationTimeDto,
    ];
  }

  factory _PurchasedSubscriptionInfoDto.fromJson(Map<String, dynamic> json) {
    final priceIncreaseStatusDto = _PurchasedSubscriptionPriceIncreaseStatusDto.fromString(
        json[_kPurchasedSubscriptionInfoPriceIncreaseStatusKey]);

    final expirationReasonString = json[_kPurchasedSubscriptionInfoExpirationReasonKey] as String?;
    _PurchasedSubscriptionExpirationReasonDto? expirationReasonDto;

    if (expirationReasonString != null) {
      expirationReasonDto =
          _PurchasedSubscriptionExpirationReasonDto.fromString(expirationReasonString);
    }

    final gracePeriodExpirationTimeString =
        json[_kPurchasedSubscriptionInfoGracePeriodExpirationTimeKey] as String?;
    _TimeDto? gracePeriodExpirationTimeDto;

    if (gracePeriodExpirationTimeString != null) {
      gracePeriodExpirationTimeDto = _TimeDto.fromString(gracePeriodExpirationTimeString);
    }

    return _PurchasedSubscriptionInfoDto._internal(
      appStoreUserId: json[_kPurchasedSubscriptionInfoAppStoreUserIdKey],
      purchaseItemSku: json[_kPurchasedSubscriptionInfoPurchaseItemSkuKey],
      originalTransactionId: json[_kPurchasedSubscriptionInfoOriginalTransactionIdKey],
      autoRenewalEnabled: json[_kPurchasedSubscriptionInfoAutoRenewalEnabledKey],
      isInBillingRetryPeriod: json[_kPurchasedSubscriptionInfoIsInBillingRetryPeriodKey],
      priceIncreaseStatusDto: priceIncreaseStatusDto,
      test: json[_kPurchasedSubscriptionInfoTestKey],
      expirationReasonDto: expirationReasonDto,
      gracePeriodExpirationTimeDto: gracePeriodExpirationTimeDto,
    );
  }

  factory _PurchasedSubscriptionInfoDto.fromEntity(
      AppStorePurchasedSubscriptionInfo purchasedSubscriptionInfo) {
    final priceIncreaseStatusDto = _PurchasedSubscriptionPriceIncreaseStatusDto.fromEntity(
        purchasedSubscriptionInfo.priceIncreaseStatus);

    _PurchasedSubscriptionExpirationReasonDto? expirationReasonDto;

    if (purchasedSubscriptionInfo.expirationReason != null) {
      expirationReasonDto = _PurchasedSubscriptionExpirationReasonDto.fromEntity(
          purchasedSubscriptionInfo.expirationReason!);
    }

    _TimeDto? gracePeriodExpirationTimeDto;

    if (purchasedSubscriptionInfo.gracePeriodExpirationTime != null) {
      gracePeriodExpirationTimeDto =
          _TimeDto.fromEntity(purchasedSubscriptionInfo.gracePeriodExpirationTime!);
    }

    return _PurchasedSubscriptionInfoDto._internal(
      appStoreUserId: purchasedSubscriptionInfo.appStoreUserId,
      purchaseItemSku: purchasedSubscriptionInfo.purchaseItemSku,
      originalTransactionId: purchasedSubscriptionInfo.originalTransactionId,
      autoRenewalEnabled: purchasedSubscriptionInfo.autoRenewalEnabled,
      isInBillingRetryPeriod: purchasedSubscriptionInfo.isInBillingRetryPeriod,
      priceIncreaseStatusDto: priceIncreaseStatusDto,
      test: purchasedSubscriptionInfo.test,
      expirationReasonDto: expirationReasonDto,
      gracePeriodExpirationTimeDto: gracePeriodExpirationTimeDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kPurchasedSubscriptionInfoAppStoreUserIdKey: appStoreUserId,
      _kPurchasedSubscriptionInfoPurchaseItemSkuKey: purchaseItemSku,
      _kPurchasedSubscriptionInfoOriginalTransactionIdKey: originalTransactionId,
      _kPurchasedSubscriptionInfoAutoRenewalEnabledKey: autoRenewalEnabled,
      _kPurchasedSubscriptionInfoIsInBillingRetryPeriodKey: isInBillingRetryPeriod,
      _kPurchasedSubscriptionInfoPriceIncreaseStatusKey: priceIncreaseStatusDto.toString(),
      _kPurchasedSubscriptionInfoTestKey: test,
      _kPurchasedSubscriptionInfoExpirationReasonKey: expirationReasonDto?.toString(),
      _kPurchasedSubscriptionInfoGracePeriodExpirationTimeKey:
          gracePeriodExpirationTimeDto?.toString(),
    };
  }

  AppStorePurchasedSubscriptionInfo toEntity() {
    return AppStorePurchasedSubscriptionInfo(
      appStoreUserId: appStoreUserId,
      purchaseItemSku: purchaseItemSku,
      originalTransactionId: originalTransactionId,
      autoRenewalEnabled: autoRenewalEnabled,
      isInBillingRetryPeriod: isInBillingRetryPeriod,
      priceIncreaseStatus: priceIncreaseStatusDto.toEntity(),
      test: test,
      expirationReason: expirationReasonDto?.toEntity(),
      gracePeriodExpirationTime: gracePeriodExpirationTimeDto?.toEntity(),
    );
  }
}
